<?php

namespace backend\models;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%file}}".
 *
 * @property int $id
 * @property int $svn_id
 * @property string $title 文件路径名
 * @property string|null $detail
 * @property string|null $created_at
 * @property string|null $updated_at
 *
 * @property SvnFile[] $svnFiles
 */
class File extends Base
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%file}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['detail', 'comment'], 'string'],
            [['created_at', 'updated_at'], 'safe'],
            [['title', 'path'], 'string', 'max' => 255],
            [['path'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'         => 'ID',
            'title'      => '文件名',
            'comment'    => '注释',
            'path'       => '文件相对路径名',
            'detail'     => 'Detail',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    /**
     * Gets query for [[SvnFiles]].
     *
     * @return \yii\db\ActiveQuery|SvnFileQuery
     */
    public function getSvnFiles()
    {
        return $this->hasMany(SvnFile::class, ['file_id' => 'id']);
    }

    /**
     * {@inheritdoc}
     * @return FileQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new FileQuery(get_called_class());
    }

    /**
     * 获取栏目的枚举值，
     * key=>value的形式组合:key表示栏目ID,value表示栏目名称
     */
    public static function dropDownList()
    {
        $query = static::find();
        $enums = $query->limit(100)->all();
        return $enums ? ArrayHelper::map($enums, 'id', 'title') : [];
    }
}
